<!--
 * @Author: cjj
 * @Date: 2024-11-18 14:53:02
 * @LastEditors: cjj
 * @LastEditTime: 2024-11-19 10:36:08
 * @Description: 2901545842@qq.com
-->
<script setup>
import { useRouter } from 'vue-router'
const router = useRouter()
import { onMounted, ref } from 'vue';
import { showNotify, showToast } from 'vant';
import { getScore } from '@/api/exchange';
import { exchangeStore } from '@/stores/exchange';
import { consumeScore } from '@/api/exchange';
const exchange = exchangeStore()
const scorevalue = ref('')
const score = ref('')
const showBottom = ref(false)
//获取所有分数
onMounted(() => {
    getScore().then(res => {
      score.value = res.data
    })
})
//兑换
function submit() {
    if (scorevalue.value < 100){
        showToast({
            message: '小于200公里，无法兑换',
            type: 'error'
        })
    }
    else{
        showBottom.value = true
    }
}
const move = ref()
const moveimg = ref()
//用来判断是否滑动
const ismove = ref(false)
//记录初始位置
let startX = 0
function movebtn(e) {
    ismove.value = true
    //记录鼠标初始位置
    startX = e.touches[0].clientX
    move.value.style.transition = 'none'
    moveimg.value.style.transition = 'none'

}
function movechange(e) {
    if (!ismove.value) return
    //记录鼠标移动距离加上初始位置
    let moveX = e.touches[0].clientX - startX + 20 
    
    if (moveX > 280){
        moveX = 280
    }
    if (moveX < 20){
        moveX = 20
    }
    //赋值给滑动块
    move.value.style.left =  moveX + 'px'
    //赋值给滑块图片
    moveimg.value.style.left = moveX +  'px'
}
function stopmove(e) {
    // if (!ismove.value) return
    if (moveimg.value.style.left < '228px' && moveimg.value.style.left > '218px') {
        showBottom.value = false
        move.value.style.left = 20 + 'px'
        moveimg.value.style.left = 20 + 'px'
        showToast({
            message: '验证通过',
            duration: 1000,
            type: 'success'
        })
        setTimeout(()=>{
            //加入记录
            exchange.addExchangeRecord({
                score:scorevalue.value,
                time:new Date().toLocaleDateString(),
            })
            //消耗积分
            consumeScore({consume:scorevalue.value}).then(res => {
            
            })
            //跳转到结果页面
            router.push({path:'/scoreresult',query:{score:scorevalue.value}})
        },1000)
    }
    move.value.style.left = 20 + 'px'
    moveimg.value.style.left = 20 + 'px'
    move.value.style.transition = 'left 1.5s'
    moveimg.value.style.transition = 'left 1.5s'
    ismove.value = false

}
</script>


<template>
    <div class="box">
        <div class="header">
            <p class="title">确认兑换</p>
            <span @click="router.go(-1)" class="back"><van-icon name="arrow-left" /></span>
            <img src="https://cdn7.axureshop.com/demo/1972680/images/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98/u2938.svg"
                alt="">
        </div>
        <div class="content">
            <div class="form">
                <van-field type="number" v-model="scorevalue" placeholder="请输入公里数" />
                <p>可兑换{{score}}公里 <span @click="scorevalue = score">全部里程</span></p>
            </div>
            <van-button :disabled="score == 0 || scorevalue == '' || scorevalue > score " round type="primary" @click="submit" style="margin-top: 30px; width: 100%;">兑换</van-button>
        </div>
        <van-popup v-model:show="showBottom" round position="bottom" :style="{ height: '30%' }">
            <div class="confirm">
                <p>请完成下方验证 <img
                        src="https://cdn7.axureshop.com/demo/1972680/images/%E7%A1%AE%E8%AE%A4%E5%85%91%E6%8D%A2/u3962.svg"
                        alt=""> </p>
                <div class="img1"> <img
                        src="https://cdn7.axureshop.com/demo/1972680/images/%E7%A1%AE%E8%AE%A4%E5%85%91%E6%8D%A2/img_u3954.svg"
                        alt="">
                    <div class="blank">

                    </div>
                    <div class="move" ref="moveimg"><img
                            src="https://cdn7.axureshop.com/demo/1972680/images/%E7%A1%AE%E8%AE%A4%E5%85%91%E6%8D%A2/u3959.svg"
                            alt=""></div>
                    <div @touchstart.passive="movebtn" @touchmove.passive="movechange" @touchend="stopmove" ref="move" class="move2">
                        滑块
                    </div>
                </div>
            </div>
        </van-popup>
    </div>
</template>

<style scoped lang="scss">
.box {
    width: 100%;
    height: 100%;

    .header {
        font-size: 18px;
        color: #fff;
        position: relative;
        background-color: rgba(22, 120, 255, 1);

        .title {
            height: 50px;
            line-height: 50px;
            text-align: center;
        }

        .back {
            position: absolute;
            top: 10px;
            left: 20px;
            font-size: 25px;
        }

        img {
            position: absolute;
            top: 12px;
            right: 20px;
            font-size: 25px;
        }
    }

    .content {
        box-sizing: border-box;
        padding: 0 20px;
        height: calc(100% - 50px);
        overflow: hidden;

        .form {
            margin-top: 30px;
            width: 100%;
            height: 100px;
            border-radius: 15px;
            box-shadow: 0px 0px 16px rgba(226, 238, 255, 1);
            color: #ccc;

            .van-field {
                height: 50px;
                padding: 0 20px;
                line-height: 50px;
            }

            p {
                border-top: 1px solid #ccc;
                box-sizing: border-box;
                padding: 0 20px;
                height: 50px;
                display: flex;
                justify-content: space-between;
                align-items: center;
                font-size: 14px;

                span {
                    color: #ff6a00;
                }
            }
        }
    }

    .van-popup--bottom {

        height: 350px !important;
        padding: 0 20px;
        box-sizing: border-box;

        .confirm {
            width: 100%;
            height: 100%;

            p {
                height: 40px;
                line-height: 40px;
                text-align: center;
                position: relative;
                font-size: 18px;
                margin-bottom: 5px;

                img {
                    position: absolute;
                    right: 20px;
                    top: 50%;
                    transform: translateY(-50%);
                }
            }

            .img1 {
                width: 100%;
                margin: 0 auto;
                position: relative;
                box-sizing: border-box;
                border: 1px solid #ccc;
                height: 290px;

                img {
                    width: 100%;
                    height: 226px;
                }

                .blank {
                    width: 48px;
                    height: 48px;
                    position: absolute;
                    background-color: rgba(255, 255, 255, 0.4);
                    left: 230px;
                    top: 63px;

                }

                .move {
                    width: 48px;
                    height: 48px;
                    position: absolute;
                    left: 20px; //225
                    top: 58px;


                    img {
                        width: 68px;
                        height: 68px;
                    }
                }

                .move2 {
                    width: 48px;
                    height: 30px;
                    position: absolute;
                    left: 20px; //225
                    bottom: 10px;
                    box-sizing: border-box;
                    border: 1px solid #ccc;
                    text-align: center;
                    line-height: 30px;
                }
            }

        }
    }

}

</style>